package com.hndzyu.yd.vip;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.bean.XW;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;
import com.hndzyu.yd.utils.Util;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zz9527 on 2018/12/5.
 */
public class XWFragment extends Fragment {
    private ListView lv;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_tj, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        SmartRefreshLayout refresh = view.findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData();
            }
        });
        lv = view.findViewById(R.id.lv_game);
        getData();

    }

    private void getData() {
        try {
            OkHttpDownloadJsonUtil.downloadJson(getContext(), Path.GAMEXW(Util.getIMEI(getContext()), UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
                @Override
                public void onsendJson(String json) {
                    try {
                        Log.i("_________",Path.GAMEXW(Util.getIMEI(getContext()), UserBean.uid)+"_"+json);
                        Gson gson = new Gson();
                        json = URLDecoder.decode(json, "UTF-8");
                        final List<XW.ItemsBean> list = gson.fromJson(json, XW.class).getItems();
                        lv.setAdapter(new XWAdapter(getContext(), list));
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                                if (UserBean.level == 0) {
//                                    final VipDIalog vipDIalog = new VipDIalog(getContext());
//                                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
//                                        @Override
//                                        public void chooseTrue() {
//                                            startActivity(new Intent(getContext(), PayVIPActivity.class));
//                                            vipDIalog.dismiss();
//                                        }
//
//                                        @Override
//                                        public void chooseFalse() {
//                                            vipDIalog.dismiss();
//
//                                        }
//                                    });
//                                    vipDIalog.show();
//                                    vipDIalog.setContent();
//                                } else {
                                    Intent intent = new Intent(getContext(), XWActivity.class);
                                    intent.putExtra("adid", list.get(i).getAdid());
                                    ArrayList<String> strlist = new ArrayList<>();
                                    strlist.add(list.get(i).getIntro());
                                    intent.putStringArrayListExtra("bq", strlist);
                                    startActivity(intent);
//                                }
                            }
                        });
                    } catch (Exception e) {
                        Toast.makeText(mContext, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } catch (Exception e) {
        }
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext= activity;
    }
    private Context mContext;
}
